<?php
class Product_Model_QualityControlResult
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_QualityControlResult();
	}
	
	public static function getInstance(){
		return new Product_Model_QualityControlResult();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet $row QualityControlResult data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row QualityControlResult data 
	*@param int $qcr_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$row['qci_last_update'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto('qcr_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $qcr_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('qcr_id = ?', $id); 
		return $this->_table->delete($where);
	}
	/*
	*根据qci_id删除
	*/
	 public function deleteByQciId($qci_id){
		$where = $this->_table->getAdapter()->quoteInto('qci_id = ?', $qci_id); 
		return $this->_table->delete($where);
	}
	
	/**
	* 
	* @param int $qci_id
	* @return zend_db_table_row
	*/
	public function getQciIdAll($qci_id){
		$where = $this->_table->getAdapter()->quoteInto("qci_id = ?",$qci_id);
		return $this->_table->fetchAll($where);
	}
	
	public function getByPovIdAndQciId($pov_id, $qci_id){
		$where = $this->_table->getAdapter()->quoteInto("pov_id = ?",$pov_id);
		$where .= $this->_table->getAdapter()->quoteInto(" and qci_id = ?",$qci_id);
		return $this->_table->fetchRow($where);	
	}
	
	/**
	* 
	* @param int $qcr_id
	* @return zend_db_table_row
	*/
	public function getByQcrId($qcr_id){
		$where = $this->_table->getAdapter()->quoteInto("qcr_id = ?",$qcr_id);
		return $this->_table->fetchRow($where);
	}
	
    public function getByCondition($condition=array(),$type='*',$orderby="qcr_id",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["qci_id"]) && $condition["qci_id"]!=''){
            $select->where(" qci_id = ?",$condition["qci_id"]);
        }
		if(isset($condition["poc_id"]) && $condition["poc_id"]!=''){
            $select->where(" poc_id = ?",$condition["poc_id"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
			
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}	
}